package com.zhiyou100.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.ResultObject;
import com.zhiyou100.model.Room;
import com.zhiyou100.service.RoomService;
import com.zhiyou100.unit.FastDFSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    RoomService roomService;

    @RequestMapping("/list")
    public String listRoom(@RequestParam Map map, @RequestParam(defaultValue = "1") int pageNum,  HttpServletRequest request){
        System.out.println("map : "+map);
        PageHelper.startPage(pageNum, 5);
        List<Room> rooms = roomService.listRoom(map);
        PageInfo<Room> pageInfo = new PageInfo<Room>(rooms);
        request.setAttribute("map",map);
        request.setAttribute("rooms",rooms);
        request.setAttribute("pageInfo",pageInfo);
        return "forward:/behind/room/listRoom.jsp";
    }
    @RequestMapping("/ajaxFileUpdate")
    @ResponseBody
    public ResultObject ajaxFileUpdate(@RequestParam MultipartFile file) throws Exception {
        System.out.println("接收到ajax文件上传请求");
        String upload = FastDFSUtil.upload(file);
        System.out.println("上传完成!");

        String data ="http://zhiyou01/"+upload;
        System.out.println("图片路径   :  "+data);

        return new ResultObject("200","上传成功",data);
    }

    @RequestMapping("/addRoom")
    public String addRoom(@RequestParam(defaultValue = "0") String strId,Room room){
        int id = Integer.parseInt(strId);
        if(id == 0){
            System.out.println("要添加的Room是   :   "+room);
            int i = roomService.addRoom(room);
            System.out.println("添加Room返回的受影响行数是   : "+i);
        }else {
            System.out.println("要更新的RoomId是 : "+id);
            int  i  = roomService.updateRoom(room);
            System.out.println("更新Room返回的受影响行数是   : "+i);
        }
        return "redirect:/room/list.do";

    }
    @RequestMapping("/lookRoom")
    public String lookRoom(int id,HttpServletRequest request){
        System.out.println("要查看详情的id是 : "+id);
        Room room =  roomService.lookRoom(id);
        request.setAttribute("room",room);
        return "forward:/behind/room/lookRoom.jsp";

    }
    @RequestMapping("/lookRoom2")
    public String lookRoom2(int id,HttpServletRequest request){
        System.out.println("要查看修改的id是 : "+id);
        Room room =  roomService.lookRoom(id);
        request.setAttribute("room",room);
        return "forward:/behind/room/addRoom.jsp";

    }
    @RequestMapping("/deleteAll")
    public String deleteAll(int[] ids){
        // System.out.println("要删除的数组是   :   "+ids.length);
        List<Integer> roomIds = new ArrayList<>();
        for (int id:ids) {
            roomIds.add(id);
        }
        // System.out.println("list集合的大小是  : "+videoIds.size());
        int i = roomService.deleteAll(roomIds);
        System.out.println("批量删除后返回的受影响行数是   :   "+i);
        return "redirect:/room/list.do";
    }
    @RequestMapping("/delete")
    private String delete(int id){
        System.out.println("要删除的id是    :    "+id);
        int i = roomService.delete(id);
        System.out.println("单个删除后返回的受影响行数是   :   "+i);
        return "redirect:/room/list.do";

    }

}
